Skip to content

Conversation

@penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Jul 10, 2025

Part 1: Adding hasvalue and getvalue to AbstractPPL
Part 2: Removing hasvalue and getvalue from DynamicPPL
Part 3: Introducing InitContext and init!!
Part 4: Using InitFromParams to implement predict, returned, and initialize_values

This is part 5/N of #967.

This PR removes everything that is no longer needed. SamplingContext, SampleFromPrior, SampleFromUniform, now have direct one-to-one replacements (albeit with slightly different behaviour since they now always overwrite variables in the varinfo).

It also removes assume and tilde_assume.

Prior to this PR we had two different kinds of assume, one with a sampler and one without. Now we only have the one without, so we can just move that definition into tilde_assume!!(::DefaultContext, ...).

Finally, tilde_assume has been subsumed into tilde_assume!! as we can just dispatch on the type of right. (Previously this wasn't possible because there was a lot of stuff about is_rhs_model, etc. etc. which was removed in #960.)

Closes #859
Closes #955

@penelopeysm penelopeysm changed the base branch from main to py/actually-use-init July 10, 2025 17:00
@penelopeysm penelopeysm force-pushed the py/remove-samplingcontext branch 2 times, most recently from c32d112 to b7221cc Compare July 10, 2025 17:16
@penelopeysm penelopeysm changed the title InitContext, part 5 - Remove SamplingContext, SampleFromPrior, SampleFromUniform, and associated code InitContext, part 5 - Remove SamplingContext, SampleFrom{Prior,Uniform}, {tilde_,}assume Jul 10, 2025
This was referenced Jul 10, 2025
@penelopeysm penelopeysm force-pushed the py/remove-samplingcontext branch from b7221cc to 3835d01 Compare July 10, 2025 17:24
@penelopeysm penelopeysm force-pushed the py/actually-use-init branch from d55d378 to a392451 Compare July 10, 2025 17:33
@penelopeysm penelopeysm force-pushed the py/remove-samplingcontext branch from 3835d01 to 713034f Compare July 10, 2025 17:42
@penelopeysm penelopeysm force-pushed the py/actually-use-init branch from a392451 to 12d93e5 Compare July 10, 2025 17:44
@penelopeysm penelopeysm force-pushed the py/remove-samplingcontext branch from 713034f to 45a97ee Compare July 10, 2025 17:45
@penelopeysm penelopeysm force-pushed the py/actually-use-init branch from 12d93e5 to 7a8e7e3 Compare July 10, 2025 17:47
@penelopeysm penelopeysm force-pushed the py/remove-samplingcontext branch from 45a97ee to e817d9c Compare July 10, 2025 17:48
@penelopeysm penelopeysm force-pushed the py/actually-use-init branch from 7e38bbe to 1d8bceb Compare July 19, 2025 22:37
@penelopeysm penelopeysm force-pushed the py/remove-samplingcontext branch from 92772b5 to 1ffc409 Compare July 19, 2025 22:39
@penelopeysm penelopeysm force-pushed the py/actually-use-init branch from 1d8bceb to 2edcd10 Compare July 20, 2025 00:59
@penelopeysm penelopeysm force-pushed the py/remove-samplingcontext branch 2 times, most recently from 1957b06 to 4fc60dc Compare July 20, 2025 17:47
@github-actions
Copy link
Contributor

github-actions bot commented Jul 20, 2025

Benchmark Report for Commit f4e5f4b

Computer Information

Julia Version 1.11.7
Commit f2b3dbda30a (2025-09-08 12:10 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 4 × AMD EPYC 7763 64-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver3)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)

Benchmark Results

|                 Model | Dimension |  AD Backend |      VarInfo Type | Linked | Eval Time / Ref Time | AD Time / Eval Time |
|-----------------------|-----------|-------------|-------------------|--------|----------------------|---------------------|
| Simple assume observe |         1 | forwarddiff |             typed |  false |                  9.0 |                 1.6 |
|           Smorgasbord |       201 | forwarddiff |             typed |  false |                679.8 |                46.1 |
|           Smorgasbord |       201 | forwarddiff | simple_namedtuple |   true |                323.9 |                74.8 |
|           Smorgasbord |       201 | forwarddiff |           untyped |   true |               1227.4 |                30.1 |
|           Smorgasbord |       201 | forwarddiff |       simple_dict |   true |               6910.3 |                29.8 |
|           Smorgasbord |       201 | reversediff |             typed |   true |               1535.0 |                29.0 |
|           Smorgasbord |       201 |    mooncake |             typed |   true |               1075.6 |                 4.6 |
|    Loop univariate 1k |      1000 |    mooncake |             typed |   true |               5944.2 |                 4.3 |
|       Multivariate 1k |      1000 |    mooncake |             typed |   true |               1037.0 |                 9.0 |
|   Loop univariate 10k |     10000 |    mooncake |             typed |   true |              66370.1 |                 4.0 |
|      Multivariate 10k |     10000 |    mooncake |             typed |   true |               9192.3 |                 9.7 |
|               Dynamic |        10 |    mooncake |             typed |   true |                136.1 |                11.8 |
|              Submodel |         1 |    mooncake |             typed |   true |                 12.9 |                 5.1 |
|                   LDA |        12 | reversediff |             typed |   true |               1205.6 |                 3.4 |

@codecov
Copy link

codecov bot commented Jul 20, 2025

Codecov Report

❌ Patch coverage is 90.90909% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.13%. Comparing base (6d43231) to head (f4e5f4b).
⚠️ Report is 3 commits behind head on breaking.

Files with missing lines Patch % Lines
src/context_implementations.jl 88.23% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           breaking     #985      +/-   ##
============================================
+ Coverage     81.20%   82.13%   +0.92%     
============================================
  Files            39       39              
  Lines          3910     3811      -99     
============================================
- Hits           3175     3130      -45     
+ Misses          735      681      -54     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link
Contributor

DynamicPPL.jl documentation for PR #985 is available at:
https://TuringLang.github.io/DynamicPPL.jl/previews/PR985/

@penelopeysm penelopeysm force-pushed the py/actually-use-init branch 2 times, most recently from 3a16f9c to 4c96020 Compare July 26, 2025 18:47
@penelopeysm penelopeysm force-pushed the py/remove-samplingcontext branch from 3951d1d to 79a5f3c Compare July 26, 2025 19:20
end
end

@testset "Turing#2151: ReverseDiff compilation & eltype(vi, spl)" begin
Copy link
Member Author

@penelopeysm penelopeysm Jul 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a way to reproduce this test without SamplingContext and the whole tilde-pipeline machinery that this PR removes, so I think that this is no longer relevant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually just for posterity I did find a way to reproduce it when I tried to remove eltype etc. in #1015. But without the changes from that PR I couldn't repro it

@penelopeysm penelopeysm force-pushed the py/remove-samplingcontext branch 2 times, most recently from 0bd4e02 to 8e3dcac Compare August 10, 2025 23:26
@penelopeysm penelopeysm force-pushed the py/actually-use-init branch from f6dd1d5 to d9292ad Compare August 13, 2025 16:51
@penelopeysm penelopeysm force-pushed the py/actually-use-init branch from 726d486 to bc04355 Compare August 13, 2025 17:16
Base automatically changed from py/actually-use-init to breaking September 18, 2025 09:29
@penelopeysm penelopeysm force-pushed the py/remove-samplingcontext branch from 8e3dcac to 0b87d0d Compare September 18, 2025 11:40
@penelopeysm penelopeysm marked this pull request as ready for review September 18, 2025 11:41
@penelopeysm penelopeysm force-pushed the py/remove-samplingcontext branch from 0b87d0d to 992569f Compare September 18, 2025 13:03
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry this file's diff is a bit of a mess, there are no real code changes, it's just:

  • merged tilde_assume, tilde_assume!!, and assume into a single function (they all just called each other)
  • added types to the arguments
  • added proper docstrings

@penelopeysm penelopeysm requested a review from mhauru September 18, 2025 13:05
Copy link
Member

@mhauru mhauru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enjoyed this one a lot.

## Model

### Macros

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it time to start a HISTORY.md entry? Might be easier to do it here were you can cross-check against what's being removed, rather than once everything is in breaking in a huge diff.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good idea to do it in this PR. I'll write one up later and ping you again

end
function tilde_assume(rng::Random.AbstractRNG, ::DefaultContext, sampler, right, vn, vi)
return assume(rng, sampler, right, vn, vi)
function tilde_assume!!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To confirm that I understand how this will play out in Turing.jl: The idea is that samplers don't need to modify the behaviour of the tilde pipeline any more, and thus SamplingContext can go in its entirety, and we don't need things like tilde_assume without !! or assume. And the few that do still need to do that (Gibbs, maybe particles, hopefully nothing else) need to define their own context. Is that right?

Copy link
Member Author

@penelopeysm penelopeysm Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, although I'm not 100% sure. We'll probably have to do a similar process to what we did last time for accumulators: make a Turing PR that builds against the breaking branch of DPPL. Things might break. But I'm hoping it won't be too bad haha.

@penelopeysm penelopeysm requested a review from mhauru September 24, 2025 14:00
@penelopeysm
Copy link
Member Author

penelopeysm commented Sep 24, 2025

Mildly tangential: I kind of find it weird that we do **Breaking changes** and then ### Subheading because the latter is bigger than the former. I feel like this was originally done to make JuliaRegistrator happy, but I checked again and it claims (e.g. JuliaRegistries/General#139062 (comment)) that it doesn't need to be **Breaking changes**, it can be ## Breaking changes. I'm a bit kind of tempted to reformat the changelog to make it look nicer (not in this PR).

Copy link
Member

@mhauru mhauru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I, too, have disliked the bolding taking precedence over headings. Happy for that to be changed.


- `InitContext(rng, InitFromPrior())`: New values are sampled from the prior distribution (on the right-hand side of the tilde).
- `InitContext(rng, InitFromUniform(a, b))`: New values are sampled uniformly from the interval `[a, b]`, and then invlinked to the support of the distribution on the right-hand side of the tilde.
- `InitContext(rng, InitFromParams(p, fallback))`: New values are obtained by indexing into the `p` object, which can be a `NamedTuple` or `Dict{<:VarName}`. If a variable is not found in `p`, then the `fallback` strategy is used, which is simply another of these strategies. In particular, `InitFromParams` enables the case where different variables are to be initialised from different sources.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enables the case where different variables are to be initialised from different sources.

Does this allow different sources in some broader sense than some having fixed values and others using fallback? Like could I do InitFromPrior for some and InitFromUniform for others?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That thought did pop into my mind when I was writing, which led me to mention the custom initialisation strategy just below this. You'd have to write your custom InitFromFoo, but it would be possible in that function to check e.g. the varname and dispatch to prior / uniform accordingly.

Co-authored-by: Markus Hauru <[email protected]>
@penelopeysm
Copy link
Member Author

Day N of coveralls failing randomly...

@penelopeysm penelopeysm merged commit d3d32e4 into breaking Sep 24, 2025
10 of 21 checks passed
@penelopeysm penelopeysm deleted the py/remove-samplingcontext branch September 24, 2025 15:48
penelopeysm added a commit that referenced this pull request Oct 21, 2025
* Bump minor version

* bump benchmarks compat

* add a skeletal changelog

* `InitContext`, part 3 - Introduce `InitContext` (#981)

* Implement InitContext

* Fix loading order of modules; move `prefix(::Model)` to model.jl

* Add tests for InitContext behaviour

* inline `rand(::Distributions.Uniform)`

Note that, apart from being simpler code, Distributions.Uniform also
doesn't allow the lower and upper bounds to be exactly equal (but we
might like to keep that option open in DynamicPPL, e.g. if the user
wants to initialise all values to the same value in linked space).

* Document

* Add a test to check that `init!!` doesn't change linking

* Fix `push!` for VarNamedVector

This should have been changed in #940, but slipped through as the file
wasn't listed as one of the changed files.

* Add some line breaks

Co-authored-by: Markus Hauru <[email protected]>

* Add the option of no fallback for ParamsInit

* Improve docstrings

* typo

* `p.default` -> `p.fallback`

* Rename `{Prior,Uniform,Params}Init` -> `InitFrom{Prior,Uniform,Params}`

---------

Co-authored-by: Markus Hauru <[email protected]>

* use `varname_leaves` from AbstractPPL instead (#1030)

* use `varname_leaves` from AbstractPPL instead

* add changelog entry

* fix import

* tidy occurrences of varname_leaves as well (#1031)

* `InitContext`, part 4 - Use `init!!` to replace `evaluate_and_sample!!`, `predict`, `returned`, and `initialize_values` (#984)

* Replace `evaluate_and_sample!!` -> `init!!`

* Use `ParamsInit` for `predict`; remove `setval_and_resample!` and friends

* Use `init!!` for initialisation

* Paper over the `Sampling->Init` context stack (pending removal of SamplingContext)

* Remove SamplingContext from JETExt to avoid triggering `Sampling->Init` pathway

* Remove `predict` on vector of VarInfo

* Fix some tests

* Remove duplicated test

* Simplify context testing

* Rename FooInit -> InitFromFoo

* Fix JETExt

* Fix JETExt properly

* Fix tests

* Improve comments

* Remove duplicated tests

* Docstring improvements

Co-authored-by: Markus Hauru <[email protected]>

* Concretise `chain_sample_to_varname_dict` using chain value type

* Clarify testset name

* Re-add comment that shouldn't have vanished

* Fix stale Requires dep

* Fix default_varinfo/initialisation for odd models

* Add comment to src/sampler.jl

Co-authored-by: Markus Hauru <[email protected]>

---------

Co-authored-by: Markus Hauru <[email protected]>

* `InitContext`, part 5 - Remove `SamplingContext`, `SampleFrom{Prior,Uniform}`, `{tilde_,}assume` (#985)

* Remove `SamplingContext` for good

* Remove `tilde_assume` as well

* Split up tilde_observe!! for Distribution / Submodel

* Tidy up tilde-pipeline methods and docstrings

* Fix tests

* fix ambiguity

* Add changelog

* Update HISTORY.md

Co-authored-by: Markus Hauru <[email protected]>

---------

Co-authored-by: Markus Hauru <[email protected]>

* fix missing import

* Shuffle context code around and remove dead code (#1050)

* Delete the `"del"` flag (#1058)

* Delete del

* Fix a typo

* Add HISTORY entry about del

* Fixes for Turing 0.41 (#1057)

* setleafcontext(model, ctx) and various other fixes

* fix a bug

* Add warning for `initial_parameters=...`

* Remove `resume_from` and `default_chain_type` (#1061)

* Remove resume_from

* Format

* Fix test

* remove initial_params warning

* Allow more flexible `initial_params` (#1064)

* Enable NamedTuple/Dict initialisation

* Add more tests

* fix include_all kwarg for predict, improve perf (#1068)

* Fix `include_all` for predict

* Fix include_all for predict, some perf improvements

* Replace `Metadata.flags` with `Metadata.trans` (#1060)

* Replace Medata.flags with Metadata.trans

* Fix a bug

* Fix a typo

* Fix two bugs

* Rename trans to is_transformed

* Rename islinked to is_transformed, remove duplication

* Change pointwise_logdensities default key type to VarName (#1071)

* Change pointwise_logdensities default key type to VarName

* Fix a doctest

* Fix DynamicPPL / MCMCChains methods (#1076)

* Reimplement pointwise_logdensities (almost completely)

* Move logjoint, logprior, ... as well

* Fix imports, etc

* Remove tests that are failing (yes I learnt this from Claude)

* Changelog

* logpdf

* fix docstrings

* allow dict output

* changelog

* fix some comments

* fix tests

* Fix more imports

* Remove stray n

Co-authored-by: Markus Hauru <[email protected]>

* Expand `logprior`, `loglikelihood`, and `logjoint` docstrings

---------

Co-authored-by: Markus Hauru <[email protected]>

* Remove `Sampler` and its interface (#1037)

* Remove `Sampler` and `initialstep`

* Actually just remove the entire file

* forgot one function

* Move sampling test utils to Turing as well

* Update changelog to correctly reflect changes

* [skip ci] Make changelog headings more consistent

---------

Co-authored-by: Markus Hauru <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants